//
//  ZCCieCleProgressView.m
//  ZhaoCaiMao
//
//  Created by wtjr on 2017/3/20.
//  Copyright © 2017年 谢英泽. All rights reserved.
//

#import "ArcProgressView.h"

@interface ArcProgressView () {
    CGFloat _W;
    CGFloat _H;
    //圆半径
    CGFloat _progressRadius;
    //弧线的宽度
    CGFloat _progressLineW;
}

@property (nonatomic, strong) UIView *roundView;
@property (nonatomic, strong) UIView *circleView;
@property (nonatomic, strong) BaseLabel *percentLabel;
@end

@implementation ArcProgressView

- (instancetype)init {
    self = [super init];
    if (self) {
        [self initData];
        [self initSubView];
    }
    return self;
}

#pragma mark - 内部方法

- (void)initData {
    _W = 80 - kUI_MarginW * 2;
    _H = _W;
    _progressLineW = 5;
    _progressRadius = (_W - _progressLineW)/2;
}

- (void)initSubView {
    //添加背景
    UIView *roundView = [[UIView alloc] initWithFrame:CGRectMake(0, 5, _W, _H)];
    self.roundView = roundView;
    [self addSubview:roundView];
    
    //添加圆路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(_W/2, _H/2) radius:_progressRadius startAngle:RADIANS_TO_DEGREES(-210) endAngle:RADIANS_TO_DEGREES(30) clockwise:YES];
    
    //添加圆弧layer背景
    CAShapeLayer *backLayer = [CAShapeLayer layer];
    backLayer.frame = CGRectMake(0, 0, _W, _H);
    backLayer.fillColor = kCOLOR_clear.CGColor;
    backLayer.strokeColor = [KCOLOR_mainColor colorWithAlphaComponent:0.5].CGColor;
    backLayer.lineCap = kCALineCapRound;
    backLayer.lineWidth = _progressLineW;
    backLayer.path = path.CGPath;
    self.backLayer = backLayer;
    [self.roundView.layer addSublayer:backLayer];
    
    //路径用CAShaper来保存
    CAShapeLayer *roundLayer = [CAShapeLayer layer];
    roundLayer.frame = CGRectMake(0, 0, _W, _H);
    roundLayer.fillColor = kCOLOR_clear.CGColor;
    roundLayer.strokeColor = KCOLOR_mainColor.CGColor;
    roundLayer.lineCap = kCALineCapRound;
    roundLayer.lineWidth = _progressLineW;
    roundLayer.path = path.CGPath;
    self.roundLayer = roundLayer;
    
    //添加渐变色
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = roundLayer.frame;
    gradientLayer.colors = @[(id)[KCOLOR_mainColor colorWithAlphaComponent:0.5].CGColor,(id)KCOLOR_mainColor.CGColor];
    gradientLayer.locations = @[@0.f,@1.f];
    gradientLayer.startPoint = CGPointMake(0, 0.5);
    gradientLayer.endPoint = CGPointMake(1, 0.5);
    gradientLayer.mask = self.roundLayer;
    self.gradientLayer = gradientLayer;
    [self.roundView.layer addSublayer:gradientLayer];
    
    //充值frame
    roundLayer.frame = gradientLayer.bounds;
    self.roundLayer.strokeEnd = 0.f;
    
    BaseLabel *percentLabel = [BaseLabel new];
    [percentLabel setLabelStyle:@"" textColor:KCOLOR_mainColor textFont:kFONT_mainBoldFont(14) texrAlignment:NSTextAlignmentCenter];
    self.percentLabel = percentLabel;
    [self.roundView addSubview:percentLabel];
    [percentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@-25);
        make.centerX.equalTo(@0);
        make.height.equalTo(@15);
    }];
}

#pragma mark - Setter && Getter

- (void)setPercent:(CGFloat)percent complete:(kPopComplete)complete {
    POPBasicAnimation *animation = [POPBasicAnimation animationWithPropertyNamed:kPOPShapeLayerStrokeEnd];
    animation.fromValue = @0;
    animation.toValue = @(percent/10.88);
    animation.duration = 1.25f;
    animation.removedOnCompletion = NO;
    animation.completionBlock = complete;
    [self.roundLayer pop_addAnimation:animation forKey:@"complete"];
}

- (void)setPercent:(CGFloat)percent {
    _percent = percent;
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    animation.fromValue = @0;
    animation.toValue = @(percent);
    animation.duration = 2;
    animation.repeatCount = 1;
    animation.fillMode = kCAFillModeForwards;
    animation.removedOnCompletion = NO;
    [self.roundLayer addAnimation:animation forKey:@"animation"];
    
    
    self.percentLabel.text = [NSString stringWithFormat:@"%.0f%%",percent * 100];
    self.percentLabel.textColor = KCOLOR_mainColor;
}

@end
